package com.gitee.huanminabc.utils_tools.web_base.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


/**
 * @author HuAnmin
 * @version 1.0
 * @email 3426154361@qq.com
 * @date 2021/3/23-15:39
 * @description 类描述....
 */
@Configuration
@EnableSwagger2     //开启Swagger2 -ui
public class SwaggerConfig {

    @Value(value = "${swagger.enabled:true}")
    private Boolean swaggerEnabled;

    @Value("${spring.application.authorizationName:'Authorization'}")
    private String authorizationName;
    @Bean  //Swagger的使用主要是要将docket对象传入IOC容器
    public Docket docket(){


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) //关于文档的各种信息
                .enable(swaggerEnabled) //使Swagger生效 项目上线的时候这个需要设置为false
                .groupName("default") //设置分组
                .select()
                //选择扫描的接口  controller
                //.apis(RequestHandlerSelectors.basePackage("com.booksadmin.controller"))//指定扫描的接口
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //包含注解的方式来确定要显示的接口(推荐)
                .build()
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());

    }
    /**
     * 安全模式，这里指定token通过Authorization头请求头传递
     */
    private List<ApiKey> securitySchemes()
    {
        List<ApiKey> apiKeyList = new ArrayList<ApiKey>();
        apiKeyList.add(new ApiKey("Authorization", authorizationName, "header"));
        return apiKeyList;
    }

    /**
     * 安全上下文
     */
    private List<SecurityContext> securityContexts()
    {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    /**
     * 默认的安全上引用
     */
    private List<SecurityReference> defaultAuth()
    {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("WEB - API")
                .build();
    }

}